/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var url = '/v1/department/score/file.json';

    var year = $('.select-year').val();

    loadList(url, year);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('.select-year').on('change', function () {
        year = $(this).val();
        loadList(url, year);
    });

    $('#normal-dialog').on('show.bs.modal', function (event) {
        $(this).find('.modal-title').text('积分');
        var modal_body = $(this).find('.modal-body');
        var id = $(event.relatedTarget).data('id');
        sendAjax('/v1/department/score/file/detail.json', 'get', {id: id}, function (json) {
            var score = json.body.score > 0 ? json.body.score : '';
            var html = '<div class="form-horizontal">';
            html += '<div class="form-group">';
            html += '<label for="inputPassword3" class="col-sm-2 control-label">积分项目</label>';
            html += '<div class="col-sm-10">';
            html += '<input type="hidden" class="file-id" value="'+json.body.id+'">';
            html += '<span class="file-content">'+json.body.content+'</span>';
            html += '</div></div>';
            html += '<div class="form-group">';
            html += '<label for="inputPassword3" class="col-sm-2 control-label">参考分数</label>';
            html += '<div class="col-sm-10">';
            html += '<span class="file-refer-score">'+json.body.refer_score+'</span>';
            html += '</div></div>';
            html += '<div class="form-group">';
            html += '<label for="inputPassword3" class="col-sm-2 control-label">打分</label>';
            html += '<div class="col-sm-10">';
            html += '<input type="text" class="form-control file-score" value="'+score+'">';
            html += '</div></div>';
            html += '</div>';
            modal_body.html(html);
        });
    }).on('hide.bs.modal', function(){
        $(this).find('.modal-body').html('');
    });

    $('.dialog-btn-save').on('click', function () {
        var data = {
            id: $('.file-id').val(),
            score: $('.file-score').val()
        };
        saveData('/v1/department/save/score/file/score', {params: data}, '', '打分完成');
    });

});

function loadList(url, year) {
    var score = self_score = 0;
    $('.data-table').bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: false,
        toolbar: '',
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: false,
        sortable: false,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        showFooter: true,
        queryParams : function (params) {
            params.year = year;
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: '序号',
            footerFormatter: function () {
                return '星级等级';
            }
        }, {
            field: 'content',
            title: '积分项目',
            footerFormatter: function (row) {
                $.each(row, function (n, v) {
                    score += v.score;
                    if (v.id > 0) {
                        self_score += v.self_score;
                    }
                });
                var star = '';
                var count = 0;
                if (score >= 90) {
                    count = 5;
                } else if (score >= 60) {
                    count = 3;
                } else {
                    count = 0;
                }
                for (var i = 1; i <= count; i ++) {
                    star += '<i class="fa fa-star"></i>';
                }
                return '<span class="text-red">'+star+'</span>';
            }
        }, {
            field: 'self_score',
            title: '自评分',
            footerFormatter: function () {
                return '总计：'+self_score;
            }
        }, {
            field: 'score_file_id',
            title: '操作',
            formatter: function (value) {
                return value == undefined ? '' : '<a class="btn btn-sm btn-info" href="#" data-id="'+value+'" data-toggle="modal" data-target="#normal-dialog"><i class="fa fa-edit"></i>自评</a>';
            }
        }]
    });
}

function refreshList() {
    $('.data-table').bootstrapTable('refresh');
}